<template>
	<view class="page">
		<u-sticky><u-tabs :list="list" active-color="rgb(241,158,194)" :is-scroll="false" :current="current" @change="change"></u-tabs></u-sticky>
		<view v-if="current == 0" class="flex box bg-wrapper">
			<view class="item" v-for="(item, index) in datalist">
				<view class="" style="position: relative;">
					<image @click="chakan(index)" :src="item.cover_image"></image>
					<view v-if="current == 0" @click="shebeijing(item.id)" class="setting">设为空间背景</view>
				</view>
			</view>
		</view>

		<view v-if="current == 1" class="wrapper">
			<view v-for="(item, index) in datalist" class="wrapper-item" style="background: url('../../static/qozne/bg.png');background-size: 100% 100%;">
				<image @click="chakan(item)" :src="item.cover_image"></image>
				<image @click="chakan(item)" :src="item.second_image"></image>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			list: [
				{
					name: '空间背景'
				},
				{
					name: '空间头像'
				}
			],
			current: 0,
			datalist: []
		};
	},
	onLoad() {
		uni.setNavigationBarColor({
			frontColor: '#000000',
			backgroundColor: '#ffffff'
		});
	},
	onShow() {
		this.media();
	},
	methods: {
		chakan(index) {
			if (this.current == 0) {
				let arr = [];
				for (let i in this.datalist) {
					arr.push(this.datalist[i].cover_image);
				}
				uni.previewImage({
					current: index, //预览图片的下标
					urls: arr //预览图片的地址，必须要数组形式，如果不是数组形式就转换成数组形式就可以
				});
			} else {
				uni.navigateTo({
					url: './touxiang?item=' + JSON.stringify(index)
				});
			}
		},
		async shebeijing(e) {
			let data = {
				my_space_id: this.vuex_user_content.id,
				space_media_id: e,
				media_type: this.current
			};
			if (this.current == 1) {
			}
			let res = await this.$u.api.change_media(data);
			uni.showToast({
				title: '设置成功',
				icon: 'none'
			});
			console.log(res);
		},
		change(index) {
			this.current = index;
			this.media();
		},
		async media() {
			let res = await this.$u.api.media({ type: this.current });
			console.log(res);
			this.datalist = res;
		}
	}
};
</script>

<style scoped lang="scss">
.page {
	height: 100vh;
	background: linear-gradient(to bottom, #fbd6e3, #a9ede9);
	overflow: auto;
}
.wrapper {
	overflow: auto;
	&-item {
		width: 90%;
		margin: 20rpx auto;
		display: flex;
		justify-content: space-around;
		align-items: center;
		padding: 40rpx 0;
		image {
			width: 300rpx;
			height: 300rpx;
			border-radius: 20rpx;
		}
	}
}
.bg-wrapper {
	display: flex;
	justify-content: space-between;
	align-items: flex-start;
	.item {
		width: 49%;
		position: relative;
		height: 461rpx;
		margin: 20rpx 0;
	}
	.setting {
		position: absolute;
		background: rgba(255, 255, 255, 0.2);
		color: #000;
		right: 20rpx;
		bottom: 20rpx;
		padding: 10rpx 20rpx;
		text-align: center;
		border-radius: 50rpx;
		font-size: 25rpx;
	}
	image {
		width: 100%;
		height: 461rpx;
		border-radius: 20rpx;
	}
}
</style>
